package com.situ.mall.order.controller;

import com.situ.mall.base.service.IAttachmentService;
import com.situ.mall.base.service.IProductService;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.IndexProductVO;
import com.situ.mall.base.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ClassName: ProductController
 * Package: com.situ.mall.order.controller
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-03-30 10:54
 * @Version 1.0
 */
@Controller
@RequestMapping(URLUtil.PRODUCT)
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IAttachmentService attachService;

    @Autowired
    private ICartService cartService;

    @GetMapping(URLUtil.HAN_FU)
    @ResponseBody
    public List<IndexProductVO> hanFu() {
        return productService.hanFu();
    }

    @GetMapping(URLUtil.DETAIL)
    public String hanFu(Integer id, Model m) {
        m.addAttribute(URLUtil.PRODUCT_HTML, productService.getById(id));
        m.addAttribute(URLUtil.ATTACHMENT_HTML, attachService.getBySourceAndSourceId(URLUtil.PRODUCT_HTML, id));
        return URLUtil.DETAIL_HTML;
    }

    @PostMapping(URLUtil.CART)
    @ResponseBody
    public void addToCart(Integer productId){
        cartService.addToCart(productId);
    }


}
